﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace FU.SE0208.G1.Controller
{
    public class WorkSessionHandler
    {
        public bool addnew(FU.SE0208.G1.Entities.WorkSession obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("AddWorkSession",conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@BeginTime", SqlDbType.DateTime)).Value = obj.BeginTime1;
                command.Parameters.Add(new SqlParameter("@EndTime", SqlDbType.DateTime)).Value = obj.EndTime1;
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else 
                    return false;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool del(FU.SE0208.G1.Entities.WorkSession obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("DeleteWorkSession", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@WorkSessID", SqlDbType.DateTime)).Value = Convert.ToInt32(obj.WorkSessID1);
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool edit(FU.SE0208.G1.Entities.WorkSession obj, FU.SE0208.G1.Entities.WorkSession newObj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("EditWorkSession", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@WorkSessID", SqlDbType.Int)).Value = Convert.ToInt32(obj.WorkSessID1);
                command.Parameters.Add(new SqlParameter("@BeginTime", SqlDbType.DateTime)).Value = newObj.BeginTime1;
                command.Parameters.Add(new SqlParameter("@EndTime", SqlDbType.DateTime)).Value = newObj.EndTime1;
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public DataSet getAll(string table)
        {
            DataSet ds = new DataSet("G1");
            DbHandler.getQuery(ds, "Select * from [" + table + "]", table);
            return ds;
        }
    }
}
